angular.module('manager', ['ionic', 'angular-meteor','angularMoment','ngCordova',  'chart.js','common',
  'global','manager.controllers','inventory', 'common.services',
  'chat'
])
.run(function($rootScope, $ionicPlatform,amMoment,StatusBarService,BackButtonService) {
  $ionicPlatform.ready(function() {
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }

    StatusBarService.setStyle();
    BackButtonService.registerBackButtonAction();
  });
  amMoment.changeLocale('zh-cn');

  // 切换state前检查是否还在线,网络是否正常
  $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, options) {
    if ($rootScope.currentUser===null) {
      $ionicPopup.confirm({
        title: '登录超时',
        template: '请您重新登录',
        cancelText: '取消',
        okText: '确定'
      }).then(function (res) {
        setTimeout(function () {
          window.location.href='index.html';
        }, 500);

      });
    }
  });

})

.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider,ChartJsProvider) {
  $ionicConfigProvider.backButton.text('');
  $ionicConfigProvider.backButton.previousTitleText(false);
  ChartJsProvider.setOptions({
    animationEasing : "easeInOutQuart",
  });
  ChartJsProvider.setOptions('Bar', {
    animationEasing : "easeOutBounce",
  });

  $ionicConfigProvider.tabs.style('standard');
  $ionicConfigProvider.tabs.position('bottom');

  //ui路由
  $stateProvider
    .state('app', {
    url: '/',
    abstract: true,
    templateUrl: 'templates/common/menu.html',
    controller: 'AppCtrl'
  })

  .state('app.home', {
    url: 'home',
    views: {
      'menuContent': {
        templateUrl: 'templates/manager/tab-home.html',
        controller: 'HomeCtrl'
      }
    }
  })
  // 我的业绩
    .state('app.performance', {
      url: 'performance',
      views: {
        'menuContent': {
          templateUrl: 'templates/sale/performance.html',
          controller: 'PerformanceCtrl'
        }
      }
    })

    // 开销售单
    .state('app.sale-create-order', {
      url: 'sale/create-order',
      views: {
        'menuContent': {
          templateUrl: 'templates/sale/create-order.html',
          controller: 'CreateSaleOrderCtrl'
        }
      }
    })
    // 销售单列表
    .state('app.sale-order-list', {
      url: 'sale/order-list-:title-:startTime-:endTime',
      views: {
        'menuContent': {
          templateUrl: 'templates/sale/order-list.html',
          controller: 'SaleOrderListCtrl'
        }
      }
    })
    // 销售单详情
    .state('app.sale-order-detail', {
      url: 'sale/order-:_id',
      views: {
        'menuContent': {
          templateUrl: 'templates/sale/order-detail.html',
          controller: 'SaleOrderDetailCtrl'
        }
      }
    })

    // 申请调货
    .state('app.transfer-products', {
      url: 'transfer-products',
      views: {
        'menuContent': {
          templateUrl: 'templates/inventory/transfer-products.html',
          controller: 'TransferProductsCtrl'
        }
      }
    })
    // 调货，库存查询结果
    .state('app.query-result', {
      url: 'query-result',
      views: {
        'menuContent': {
          templateUrl: 'templates/inventory/query-result.html',
          controller: 'QueryResultCtrl'
        }
      }
    })
    // 历史调货列表
    .state('app.transfer-list', {
      url: 'transfer-list',
      views: {
        'menuContent': {
          templateUrl: 'templates/inventory/transfer-list.html',
          controller: 'TransferListCtrl'
        }
      }
    })
    // 调货详情
    .state('app.transfer-detail', {
      url: 'transfer-detail/1',
      views: {
        'menuContent': {
          templateUrl: 'templates/inventory/transfer-detail.html',
          controller: 'TransferDetailCtrl'
        }
      }
    })

    // 开退货
    .state('app.create-return', {
      url: 'create-return',
      views: {
        'menuContent': {
          templateUrl: 'templates/manager/sale/create-return.html',
          controller: 'CreateReturnCtrl'
        }
      }
    })
  ;
  $urlRouterProvider.otherwise('home');
});
